package com.huifaapp.mall.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huifaapp.mall.activity.AlreadyPayActivity;
import com.huifaapp.mall.activity.DetailsofPaymentActivity;
import com.huifaapp.mall.activity.WaitPayActivity;
import com.huifaapp.mall.afinal.Constants;
import com.huifaapp.mall.afinal.JSConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.MessageEvent;
import com.huifaapp.mall.uitils.Contig;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new View(this));

        api = WXAPIFactory.createWXAPI(this, Constants.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case 0:
                Intent intent = new Intent();
                intent.setAction("fbPayAction");
    //          intent.setAction("goodsPayAction");
    //          intent.setAction("integraPayAction");
                sendBroadcast(intent);
                ToastUtils.showShortSafe("支付成功");
                Intent in=new Intent(WXPayEntryActivity.this,AlreadyPayActivity.class);
                Log.e("orderNoorderNo",DetailsofPaymentActivity.orderNo+"         "+Contig.orderNo);
                in.putExtra("orderNo", Contig.orderNo);
                startActivity(in);
//              EventBus.getDefault().post(new MessageEvent().setMesg(ApiManager.BASE_HTML_URL+ JSConstants.HTML_PAY_SUCCEDD));
                break;
            case -1:
                ToastUtils.showShortSafe("支付失败");
                Intent inte=new Intent(WXPayEntryActivity.this,WaitPayActivity.class);
                Log.e("orderNoorderNo",DetailsofPaymentActivity.orderNo+"         "+Contig.orderNo);
                inte.putExtra("orderNo", DetailsofPaymentActivity.orderNo);
                startActivity(inte);
                break;
            case -2:
                ToastUtils.showShortSafe("取消支付");
                Intent insb=new Intent(WXPayEntryActivity.this,WaitPayActivity.class);
                Log.e("orderNoorderNo",DetailsofPaymentActivity.orderNo+"         "+Contig.orderNo);
                insb.putExtra("orderNo", DetailsofPaymentActivity.orderNo);
                startActivity(insb);
                break;
        }
            finish();
        }




    }